//
//  MGSBaseViewVCtrl.swift
//  Mangosteen
//
//  Created by gby on 2022/12/15.
//

import UIKit
import MZTool

extension UIViewController {
    @objc open func pushback() {
        self.navigationController?.popViewController(animated: true)
    }
}


class BaseViewVCtrl: UIViewController {
    
    open var opEmptyBgView: UIView?
    private var shadowView: UIView?
    public lazy var customNav: MZNaviHeader = MZNaviHeader.init(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.width, height: MZ.Navi_Height()))
    
    deinit {
        MZ.printLog("------------"+"\(type(of: self))")
    }
        
    override func viewDidLoad() {
        super.viewDidLoad()
        //MARK: 默认背景
        self.view.backgroundColor = MZ.RGB("#FFFFFF")
        //MARK: 设置导航栏
        self.navigationController?.navigationBar.shadowImage = UIImage.init()
        self.navigationController?.navigationBar.isHidden = false
        self.navigationController?.navigationBar.isTranslucent = true
        self.extendedLayoutIncludesOpaqueBars = true
        self.edgesForExtendedLayout = []
        
        if #available(iOS 13.0, *) {
            let appearance: UINavigationBarAppearance = UINavigationBarAppearance.init()
            appearance.backgroundColor = UIColor.white
            appearance.shadowColor = UIColor.clear
            appearance.shadowImage = UIImage.init()
            appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.black,NSAttributedString.Key.font:UIFont.mz.size(18)]
            appearance.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.black,NSAttributedString.Key.font:UIFont.mz.size(18)]
            self.navigationController?.navigationBar.scrollEdgeAppearance = appearance
            self.navigationController?.navigationBar.standardAppearance = appearance
        } else {
            
        }
        self.navigationController?.navigationBar.backgroundColor = UIColor.white
        self.navigationController?.navigationBar.setBackgroundImage(UIImage.mz.color(color: UIColor.white, frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: MZ.Navi_Height())), for: UIBarPosition.topAttached, barMetrics: UIBarMetrics.default)
        self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.black,NSAttributedString.Key.font:UIFont.mz.size(18)]
        
        self.settingLeftAction()
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        self.view.endEditing(true)
    }
    
    //MARK: 设置返回按钮
    func settingLeftAction() {
        if self.navigationController?.viewControllers.count ?? 0 > 1 {
            self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(image: MZ.IMG("navi_back_black")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(pushback))
        }
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle{
        //MARK: 取消暗黑模式
        if #available(iOS 13.0, *) {
            return .darkContent
        } else {
            return .default
        }
    }
    
    public func registerCustomNav(){
        self.view.addSubview(customNav)
        self.view.bringSubviewToFront(customNav)
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
    }
    
    
//    func createEmptyBgViewWith(empty: RCEmptyViewOC?){
//        opEmptyBgView = UIView.init()
//        opEmptyBgView?.frame = self.view.bounds
//        opEmptyBgView?.rc_emptyView = empty
//        self.view.addSubview(opEmptyBgView!)
//    }
    
//    func createNaviShadow() {
//        if self.shadowView == nil {
//            self.shadowView = UIView.init(frame: CGRect(x: 0, y: -9, width: self.view.mgs.width, height: 10))
//            self.shadowView?.backgroundColor = UIColor.white
//            MZ.mgsShadow(self.shadowView!, 0)
//        }
//        self.view.addSubview(self.shadowView!)
//        self.view.bringSubviewToFront(self.shadowView!)
//    }
    
//    func removeNaviShadow() {
//        self.shadowView?.removeFromSuperview()
//    }
}
